﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeEdu.BLL;
using LeEdu.MODEL;
using System.IO;
using System.Net.Mail;
using LeEdu.Web.Common;

namespace LeEdu.Web.Controllers
{
    public class ClassController : Controller
    {
        #region 实例化BLL层
        /// <summary>
        /// 实例化CourseBLL
        /// </summary>
        CourseBLL coursebll = new CourseBLL();

        /// <summary>
        /// 实例化Course_ClassBLL
        /// </summary>
        Course_ClassBLL course_classbll = new Course_ClassBLL();

        /// <summary>
        /// 实例化RecordBLL
        /// </summary>
        RecordBLL recorbll = new RecordBLL();

        /// <summary>
        /// 实例化UserInfoBLL
        /// </summary>
        UserInfoBLL userinfobll = new UserInfoBLL();
        #endregion

        #region 首页
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region 班级管理数据+分页查询
        /// <summary>
        /// 班级管理数据+分页查询
        /// </summary>
        /// <param name="name">班级名称</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns></returns>
        public ActionResult ClassRoom(string name, int pageIndex = 1)
        {

            BLL.ClassTableBLL bll = new BLL.ClassTableBLL();
            List<MODEL.ClassTable> lst = bll.getEntityList(p => true).Where(p => string.IsNullOrEmpty(name) ? true : p.CTName.Contains(name)).ToList();
            int pageSize = 5;
            int count = lst.Count();
            int pageCount = count / pageSize;
            if (count % pageSize > 0)
            {
                pageCount += 1;
            }
            ViewBag.pageCount = pageCount;
            ViewBag.pageIndex = pageIndex;
            ViewBag.name = name;
            lst = lst.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
            ViewBag.lst = lst;
            ViewBag.cc = course_classbll.getEntityList(p => true);
            return View();
        }
        #endregion

         #region 取消推荐状态
        /// <summary>
        /// 取消推荐状态
        /// </summary>
        /// <param name="id">班级ID</param>
        /// <returns></returns>
        public ActionResult QuXiao(int id)
        {
            BLL.ClassTableBLL bll = new BLL.ClassTableBLL();
            MODEL.ClassTable ct = bll.Search(id);
            ct.ClassReNumber = 0;
            bll.Edit(ct);
            return RedirectToAction("ClassRoom");
        }
        #endregion

        #region 推荐班级视图
        /// <summary>
        /// 查找班级对象
        /// </summary>
        /// <param name="id">班级ID</param>
        /// <returns></returns>
        public ActionResult ClassTuiShe(int id)
        {
            BLL.ClassTableBLL bll = new BLL.ClassTableBLL();
            LeEdu.MODEL.ClassTable ct = bll.Search(id);
            return View(ct);
        }
        #endregion

        #region 推荐班级
        /// <summary>
        /// 推荐班级
        /// </summary>
        /// <param name="ct">班级对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ClassTuiShe(ClassTable ct)
        {
            ClassTableBLL bll = new ClassTableBLL();
            int id = ct.ClassTableId;
            ClassTable ctb = bll.Search(id);
            ctb.ClassReNumber = Convert.ToInt32(Request["ClassReNumber"]);
            bll.Edit(ctb);
            return RedirectToAction("ClassTui");
        }
        #endregion

        #region 关闭班级
        /// <summary>
        /// 关闭班级
        /// </summary>
        /// <param name="id">班级ID</param>
        /// <returns></returns>
        public ActionResult GuanBi(int id)
        {
            ClassTableBLL bll = new ClassTableBLL();
            ClassTable ct = bll.Search(id);
            ct.ClassState = 0;
            bll.Edit(ct);
            return RedirectToAction("ClassRoom");
        }
        #endregion

        #region 班级发布
        /// <summary>
        /// 班级发布
        /// </summary>
        /// <param name="id">班级ID</param>
        /// <returns></returns>
        public ActionResult Fabu(int id)
        {
            ClassTableBLL bll = new ClassTableBLL();
            ClassTable ct = bll.Search(id);
            ct.ClassState = 1;
            bll.Edit(ct);
            return RedirectToAction("ClassRoom");
        }
        #endregion
              
        #region 班级管理推荐
        /// <summary>
        /// 班级管理推荐
        /// </summary>
        /// <returns></returns>
        public ActionResult ClassTui()
        {
            ClassTableBLL bll = new ClassTableBLL();
            List<ClassTable> ct = bll.getEntityList(p => true).Where(p => p.ClassReNumber != 0).ToList();

            ViewBag.ct = ct;
            return View();
        }
        #endregion

        #region 创建班级视图
        /// <summary>
        /// 创建班级视图
        /// </summary>
        /// <returns></returns>
        public ActionResult ClassCourseAdd()
        {
            return View();
        }
        #endregion

        #region 创建班级
        /// <summary>
        /// 创建班级
        /// </summary>
        /// <returns></returns>
        public ActionResult ClassCreate()
        {
            BLL.CourseBLL bll = new BLL.CourseBLL();

            List<MODEL.Courses> lst = bll.getEntityList(p => true).ToList();
            ViewBag.CourseId = lst;
            return View();
        }
        #endregion

        #region 添加班级
        /// <summary>
        /// 添加班级
        /// </summary>
        /// <param name="ct">班级对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddClass(MODEL.ClassTable ct)
        {
            ct.ClassCreateTime = DateTime.Now;
            ct.ClassReTime = DateTime.Now;
            ct.AddStuTime = DateTime.Now;
            BLL.ClassTableBLL bll = new BLL.ClassTableBLL();
            if (bll.AddEntity(ct))
            {
                int id = ct.ClassTableId;
                Session["cid"] = ct.ClassTableId;
                return Content("<script>;location.href='/Class/ClassManage?id=" + id + "'</script>");
            }
            else
            {
                return Content("<script>;location.href='/Class/ClassManage'</script>");
            }
        }
        #endregion

        #region 班级管理首页
        /// <summary>
        /// 班级管理首页
        /// </summary>
        /// <param name="id">班级ID</param>
        /// <returns></returns>
        public ActionResult ClassManage(int id)
        {
            Session["cid"] = id;
            ClassTableBLL bll = new ClassTableBLL();
            ClassTable cs = bll.Search(id);
            ViewBag.cm = cs;
            return View();
        }
        #endregion

        #region 班级基本信息
        /// <summary>
        /// 班级基本信息
        /// </summary>
        /// <returns></returns>
        public ActionResult ClassBasic()
        {
            int id = Convert.ToInt32(Session["cid"]);
            BLL.ClassTableBLL bll = new BLL.ClassTableBLL();
            ClassTable st = bll.Search(id);
            BLL.CollegeBLL bll1 = new BLL.CollegeBLL();
            List<MODEL.College> stu = bll1.getEntityList(t => true).ToList();
            ViewBag.CollegeId = stu;
            return View(st);
        }
        #endregion

        #region 添加班级基本信息
        /// <summary>
        /// 添加班级基本信息
        /// </summary>
        /// <param name="cla">班级对象</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ClassBasic(ClassTable cla)
        {

            BLL.ClassTableBLL bll = new BLL.ClassTableBLL();
            if (bll.Edit(cla))
            {
                return Content("<script>;location.href='/Class/ClassManage/?id=" + Convert.ToInt32(Session["cid"]) + "'</script>");
            }
            else
            {
                return Content("<script>;location.href='/Class/ClassBasic/?id=" + Convert.ToInt32(Session["cid"]) + "'</script>");
            }
        }
        #endregion

        #region 价格设置页面
        /// <summary>
        /// 价格设置页面
        /// </summary>
        /// <returns></returns>
        public ActionResult ClassPrice()
        {
            ClassTableBLL bll = new ClassTableBLL();
            int id = Convert.ToInt32(Session["cid"]);
            ClassTable ct = bll.Search(id);
            return View(ct);
        }
        #endregion

        #region 价格设置
        /// <summary>
        /// 价格设置
        /// </summary>
        /// <returns></returns>
        public string ClassPrice1()
        {
            ClassTableBLL bll = new ClassTableBLL();
            int id = Convert.ToInt32(Request["id"]);
            ClassTable ct = bll.Search(id);
            decimal price = Convert.ToDecimal(Request["price"]);
            ct.CTPrice = price;
            if (bll.Edit(ct))
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }
        #endregion

        #region 图片设置页面
        /// <summary>
        /// 图片设置页面
        /// </summary>
        /// <returns></returns>
        public ActionResult ClassPicture()
        {
            int id = Convert.ToInt32(Session["cid"]);
            BLL.ClassTableBLL bll = new BLL.ClassTableBLL();
            ClassTable st = bll.Search(id);
            return View(st);
        }
        #endregion

        #region 课程图片设置
        /// <summary>
        /// 课程图片设置
        /// </summary>
        /// <param name="gt">班级对象</param>
        /// <param name="ClassPicture">课程图片</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ClassPicture(ClassTable gt, HttpPostedFileBase ClassPicture)
        {
            int id = Convert.ToInt32(Session["cid"]);
            BLL.ClassTableBLL bll = new BLL.ClassTableBLL();

            if (ClassPicture == null)
            {
                bll.Edit(gt);
                return Content("<script>;location.href='/Class/ClassPicture/?id=" + Convert.ToInt32(Session["cid"]) + "'</script>");
            }
            else
            {
                gt.AddStuTime = DateTime.Now;
                if (ClassPicture != null)
                {
                    if (ClassPicture.ContentLength > 0)
                    {
                        string FileNmae = DateTime.Now.ToString("yyyyMMddhhmmss") + Path.GetExtension(ClassPicture.FileName);
                        string filePath = Path.Combine(Server.MapPath("/Content/images"), FileNmae);
                        ClassPicture.SaveAs(filePath);
                        gt.ClassPicture = FileNmae;
                    }
                }
                else
                {
                    gt.ClassPicture = Request["ClassPicture"].ToString();
                }

                if (bll.Edit(gt))
                {

                return Content("<script>;location.href='/Class/ClassManage/?id=" + Convert.ToInt32(Session["cid"]) + "'</script>");

                }
                else
                {
                    return Content("<script>;location.href='/Class/ClassPicture/?id=" + Convert.ToInt32(Session["cid"]) + "'</script>");
                }
            }
        }
        #endregion

        #region 课程管理
        /// <summary>
        /// 课程管理
        /// </summary>
        /// <returns></returns>
        public ActionResult ClassCourse()
        {
            int id = Convert.ToInt32(Session["cid"]);
            List<Courses> clist = coursebll.getEntityList(p => true).ToList();
            List<Course_Class> cclist = course_classbll.getEntityList(p => true).Where(p => (p.ClassTableId == id)).ToList();
            ViewBag.clist = clist;
            ViewBag.cclist = cclist;
            return View();
        }
        #endregion

        #region Ajax查询分页,判断有多少条数据
        /// <summary>
        /// Ajax查询分页,判断有多少条数据
        /// </summary>
        /// <returns></returns>
        public int ClassCourse1()
        {
            string name = Request["name"] == null ? "" : Request["name"];
            int count = coursebll.getEntityList(p => true).Where(p => p.CoursesName.Contains(name)).Count();
            return count;
        }
        #endregion

        #region Ajax查询分页
        /// <summary>
        /// Ajax查询分页
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowClassCourse()
        {
            int pageIndex = Convert.ToInt32(Request["pageIndex"]);
            int pageSize = Convert.ToInt32(Request["pageSize"]);
            string name = Request["name"] == null ? "" : Request["name"];
            List<Courses> clist = coursebll.getEntityList(p => true).Where(p => p.CoursesName.Contains(name)).ToList();
            clist = clist.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
            return Json(clist, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 学员管理
        /// <summary>
        /// 学员管理
        /// </summary>
        /// <returns></returns>
        public ActionResult ClassStudent()
        {
            
                RecordBLL recordbll = new RecordBLL();
                if (Session["Uname"] == null)
                {
                    return Content("<script>alert('请先登录');location.href='/Login/Index'</script>");
                }
                else
                {
                    List<Record> rd = recordbll.getEntityList(p => true);
                    ViewBag.rd = rd;
                    UserInfoBLL userbll = new UserInfoBLL();
                    UserInfo uf = userbll.Search(Convert.ToInt32(Session["id"]));
                    ViewBag.uf = uf;
                    return View(ViewBag.rd);
                }
            
            
        }
        #endregion

        #region 查询学员
        /// <summary>
        /// 查询学员
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ClassSou()
        {
            string name = Request["name"];
            RecordBLL recordbll = new RecordBLL();

            List<Record> rd = recordbll.getEntityList(p => true).Where(p => string.IsNullOrEmpty(name) ? true : p.UserInfo.Uname.Contains(name)).ToList();
            ViewBag.rd = rd;
            return View("ClassStudent", ViewBag.rd);
        }
        #endregion

        #region 查询用户是否存在
        /// <summary>
        /// 查询用户是否存在
        /// </summary>
        /// <returns></returns>
        public ActionResult Select()
        {
            string youxiang = Request["youxiang"];
            UserInfoBLL userbll = new UserInfoBLL();
            UserInfo uf = userbll.getEntity(p => p.Email == youxiang);
            int uid = uf.UserInfoId;
            Session["uid"] = uid;
            if (uf == null)
            {
                return Content("0");
            }
            else
            {
                return Content("1");
            }
        }
        #endregion

        #region 学员记录添加
        /// <summary>
        /// 学员记录添加
        /// </summary>
        /// <param name="rd">学员记录对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ClassStudent(Record rd)
        {
            RecordBLL recordbll = new RecordBLL();
            if (Session["id"] == null)
            {
                return Content("<script>alert('请先登录');location.href='/Login/Index';</script>");
            }
            else
            {
                rd.UserInfoId = Convert.ToInt32(Session["uid"]);
                recordbll.AddEntity(rd);
            }
            return RedirectToAction("ClassStudent");
        }
        #endregion 

        #region 服务设置
        /// <summary>
        /// 服务设置
        /// </summary>
        /// <returns></returns>
        public ActionResult ClassService()
        {
            return View();
        }
        #endregion

        #region 班主任设置
        /// <summary>
        /// 班主任设置
        /// </summary>
        /// <returns></returns>
        public ActionResult ClassMaster()
        {
            return View();
        }
        #endregion 

        #region 教师设置
        /// <summary>
        /// 教师设置
        /// </summary>
        /// <returns></returns>
        public ActionResult ClassTeacher()
        {
            return View();
        }
        #endregion

        #region 批量添加课程
        /// <summary>
        /// 批量添加课程
        /// </summary>
        /// <returns></returns>
        public ActionResult AllAddCourse()
        {
            Course_Class cc = new Course_Class();
            string[] ids = Request["ids"].Split(',');
            foreach (var item in ids)
            {
                cc.ClassTableId = Convert.ToInt32(Session["cid"]);
                cc.CoursesId = Convert.ToInt32(item);
                course_classbll.AddEntity(cc);
            }
            return Content("1");
        }
        #endregion

        #region 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <returns></returns>
        public ActionResult AllDelCourse()
        {
            string[] allids = Request["allids"].Split(',');
            for (int i = 0; i < allids.Length - 1; i++)
            {
                Course_Class cc = new Course_Class();
                cc = course_classbll.Search(Convert.ToInt32(allids[i]));
                course_classbll.Del(cc);
            }
            return Content("1");
        }
        #endregion

        #region  学员发送私信模态框
        /// <summary>
        /// 学员发送私信模态框
        /// </summary>
        /// <param name="id">学员ID</param>
        /// <returns></returns>
        public ActionResult Sx(int id)
        {
            Record findrecord = recorbll.Search(id);
            ViewBag.findrecord = findrecord;
            return View();
        }
        #endregion

        #region 给学员发送私信
        /// <summary>
        /// 给学员发送私信
        /// </summary>
        /// <param name="user">用户对象</param>
        /// <returns></returns>
        public ActionResult SendEmail(UserInfo user)
        {
            if (userinfobll.Edit(user))
            {
                MailMessage msg = new MailMessage();
                msg.To.Add(user.EmailAddress);//收件人邮箱
                msg.From = new MailAddress("2403207775@qq.com", "乐知教育", System.Text.Encoding.UTF8);
                msg.Subject = "班级提示";//邮件标题
                msg.SubjectEncoding = System.Text.Encoding.UTF8;
                msg.Body = user.EmailContent;//内容 
                msg.BodyEncoding = System.Text.Encoding.UTF8;//内容编码 
                msg.IsBodyHtml = false;//是否是HTML邮件 

                msg.Priority = MailPriority.High;//优先级 
                SmtpClient client = new SmtpClient();//smtp协议发送邮件
                client.Credentials = new System.Net.NetworkCredential("2403207775@qq.com", "yl950421");//授权密码
                client.Host = "smtp.qq.com";
                client.Send(msg);
                return Content("<script>location.href='/Class/ClassStudent'</script>");
            }
            else
            {
                return Content("<script>location.href='/Class/ClassStudent'</script>");
            }
        }
        #endregion

        #region 学员备注模态框
        /// <summary>
        /// 学员备注模态框
        /// </summary>
        /// <param name="id">学员id</param> 
        /// <returns></returns>
        public ActionResult ShowStuNotes(int id)
        {
            Record findrecord = recorbll.Search(id);
            ViewBag.findrecord = findrecord;
            return View();
        }
        #endregion

        #region 学员备注信息
        /// <summary>
        /// 学员备注信息
        /// </summary>
        /// <param name="record">学员对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditStuNotes(Record record)
        {
            if (recorbll.Edit(record))
            {
                return Content("<script>location.href='/Class/ClassStudent'</script>");
            }
            else
            {
                return Content("<script>location.href='/Class/ClassStudent'</script>");
            }
        }
        #endregion

        #region 删除班级中的学员
        /// <summary>
        /// 删除班级中的学员
        /// </summary>
        /// <param name="id">学员ID</param>
        /// <returns></returns>
        public ActionResult DelStuden(int id)
        {
            Record record = recorbll.Search(id);
            if (recorbll.Del(record))
            {
                return Content("<script>location.href='/Class/ClassStudent'</script>");
            }
            else
            {
                return Content("<script>location.href='/Class/ClassStudent'</script>");
            }
        }
        #endregion

    }
}